/*
 *  dna_license.h -- provide dna-system license management.
 *  
 *  ORIGINAL AUTHOR: Xu Chun (chun.xu@broadlink.com.cn)
 *
 *  Copyright (c) 2016 Broadlink Corporation
 */

#ifndef __DNA_LICENSE_H
#define __DNA_LICENSE_H

#ifdef __cplusplus
    extern "C" {
#endif

/* dna-system license HTTP activate type code */
enum {
    DNA_LICENSE_HTTP_ACTIVATE_WX        = 1,        /* Wechat activate */
    DNA_LICENSE_HTTP_ACTIVATE_BL,                   /* Broadlink activate */
    DNA_LICENSE_HTTP_ACTIVATE_AL,                   /* Alink (SDS) activate */
    DNA_LICENSE_HTTP_ACTIVATE_SESSION,              /* SESSION activate */
    DNA_LICENSE_HTTP_ACTIVATE_SIG,                  /* SIG activate */
    DNA_LICENSE_HTTP_ACTIVATE_NEWDNA,
    DNA_LICENSE_HTTP_ACTIVATE_QQ,
    DNA_LICENSE_HTTP_ACTIVATE_MAX,
};


/* dna-system license activate result code */
enum {
    DNA_LICENSE_ACTIVATE_OK             = 0,
    DNA_LICENSE_ACTIVATE_IGNORE,
    DNA_LICENSE_ACTIVATE_FAIL,
};

#define   DNA_SECURITY_MAGIC         (0x2df7fd2)

typedef struct security_header{
    unsigned int magic; //==0x2df7fd2
    unsigned short version; //?????o? == 1
    unsigned short len; //??Header + payLoad???1??3??
    unsigned int nonce; // o???D㡧Do?
    unsigned char session[64];
} security_header_t;
 
    typedef struct dna_security_info {
    unsigned int security_type;
    unsigned char nonce[32];
    unsigned int  devnonce;
    char session[64];
    char cloudpubkey[32];
    char key[16];
    char iv[16];
    char sharedkey[32];
    char fkey[16];
    char fiv[16];
    char dpub[32];
    char dpri[32];
} dna_security_info_t;

/*
*  dna-system license assert interface.
*
*  Note: some entry function (such as start/init) must call this interface to check 
*  License whether verify success.
*
*  Return 0 means assert success.
*/
int dna_license_assert(void);

/*
*  dna-system license verify interface.
*
*  Return 0 means verify success.
*/
int dna_license_verify(void);

/*
*  dna-system license validity check in the factory.
*
*  @sig: signature, 64bytes
*  @data: MD5 value, 16bytes
*
*  Return 0 means verify success.
*/
int dna_manulic_verify(char * sig, char * data);

/*
*  dna-system license activate request interface.
*  It used for create activate request packet.
*
*  @fwstats: firmware status
*  @output: output buffer
*  @maxsize: output buffer max size
*
*  Return output buffer valid length.
*/
int dna_license_activate_req(const char * fwstats, unsigned char * output, unsigned int maxsize);

/*
*  dna-system license activate response process interface.
*  It used for parsing activate response packet.
*
*  @input: input data
*  @len: input data valid length
*
*  Return activate result code.
*/
int dna_license_activate_res(unsigned char * input, unsigned int len);

/*
*  dna-system license HTTP activate process interface.
*
*  @type: activate request type
*  @url: activate request URL (optional, can set to NULL)
*  @userdata: activate request user data (format: "key1":"value1","key2":"value2", ...)
*  @response: activate response data
*  @maxsize: response buffer maxsize
*
*  Return activate result code.
*/
int dna_license_http_activate(int type,
        const char * url, const char * userdata,
        char * response, int maxsize)DNA_COMPILER_SECTION_SRAM;

int dna_security_certificate_encrypt(  
        const char * input, int size,
        char * output, int maxsize);
     
     
int dna_security_certificate_decrypt(
        const char * input, int size,
        char * output, int maxsize);
     
     
int dna_security_certificate(void);         
     
unsigned int dna_security_increment_nonce(void);

#ifdef __cplusplus
}
#endif

#endif

